package com.document;

import java.io.File;
import java.util.ArrayList;

import com.CreatPDF.browse;
import com.DBHelper.MySQLIteOpenHelper;
import com.document.R; 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

//创建PDF文档
public class Creat_By_LocalFile extends MyActivity {
public static Button Select_Img;
private Button Yes_To_Creat;
private Button Look_Creat_File;
private Button Back;
private EditText Show_Img_File;
private EditText File_name;
String pdfUrl;

ArrayList<String> imageUrllist=new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.creat_by_locaifile);
  
	Select_Img=(Button)findViewById(R.id.select_img);
	Yes_To_Creat=(Button)findViewById(R.id.yes_to_creat);
	Look_Creat_File=(Button)findViewById(R.id.look_creat_file);
	Back=(Button)findViewById(R.id.back);
	Show_Img_File=(EditText)findViewById(R.id.show_img_file);
	File_name=(EditText)findViewById(R.id.file_name);

	
	Select_Img.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			Intent intent=new Intent();
			intent.setClass(Creat_By_LocalFile.this, browse.class);
			startActivity(intent);
			finish();
			
				
		}
	});
	
	Show_Img_File.setText(browse.getfilepath());		

	Yes_To_Creat.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
			String selectsql="select * from FileName";
			//查询数据库数据 得到文件路径集合
			imageUrllist=helper.select(selectsql);
			
			String filename=File_name.getText().toString();
			pdfUrl = GetPath.getpath()+File.separator+filename+".pdf";
			if(!((Show_Img_File.getText()).equals("")||filename.equals(""))){
			com.CreatPDF.CreatPdfFromImg.Pdf(imageUrllist, pdfUrl);
			File mPdfFile = new File(pdfUrl);	
			//创建完成后，清空数据表
			 helper.delete("delete from FileName");
			 helper.close();
			Toast.makeText(getApplicationContext(), "创建文件成功，可以在"+GetPath.getpath()+File.separator+"路径下查看", Toast.LENGTH_LONG).show();
			
			}
			else{
				Toast.makeText(getApplicationContext(), "创建文件失败，请选择图片和输入文件名称", Toast.LENGTH_LONG).show();
			}
			
		}
	});
	Look_Creat_File.setOnClickListener(new OnClickListener() {

		public void onClick(View v) {
			if(pdfUrl!=null)
			{
				 MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
					//离开界面，清空数据表
					 helper.delete("delete from FileName");
					 helper.close();
					 startActivity(getFileIntent.getPdfFileIntent(pdfUrl));
					 finish();}
			else{
				Toast.makeText(Creat_By_LocalFile.this, "你还没创建文件", Toast.LENGTH_LONG).show();
			}
			
			
		}
	});
	Back.setOnClickListener(new OnClickListener() {
	
		public void onClick(View v) {
			//放弃创建 也要先清空，防止下次创建时，图片重复
     	   MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
				 helper.delete("delete from FileName");
				 helper.close();
			
		Intent i=new Intent();
		i.setClass(Creat_By_LocalFile.this, MainActivity.class);
		startActivity(i);
		finish();
		}
	});
	
	
	
	
}



}
